package com.ca.ridersclubapplication.view.list;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.adapter.ClubSquareAdapter;
import com.ca.ridersclubapplication.data.bean.MiniClubInfo;

import java.util.ArrayList;
import java.util.List;

public class ClubSquareListView extends ConstraintLayout {

    private Activity activity;

    private List<MiniClubInfo> list;

    public TextView labelView;
    public RecyclerView rv;
    public final ClubSquareAdapter adapter = new ClubSquareAdapter(null);

    public ClubSquareListView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public ClubSquareListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ClubSquareListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {

        View view = LayoutInflater.from(context).inflate(R.layout.club_square_list_layout, this);
        labelView = view.findViewById(R.id.tv_label);
        rv = view.findViewById(R.id.rv);

        labelView.setText("广场");

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv.setLayoutManager(layoutManager);
        rv.setAdapter(adapter);

        setList(list);
    }

    public List<MiniClubInfo> getList() {
        return list;
    }

    public void setList(List<MiniClubInfo> list) {
        this.list = list == null ? new ArrayList<>() : list;
        adapter.setList(this.list);
        if (this.list.size() == 0) {
            this.setVisibility(GONE);
        }
        else {
            this.setVisibility(VISIBLE);
        }
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
        adapter.setActivity(activity);
    }
}
